package com.example.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 */

@Slf4j
@ControllerAdvice
public class ConfigException {
    StringBuilder builder;

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public String exceptionHandler(Exception exceptionMsg) {
        final String returnExceptionMsg = builder.append("全局异常捕获")
                .append(exceptionMsg.toString()).toString();
        log.error(returnExceptionMsg);
        return returnExceptionMsg;
    }


    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public String isNullException(NullPointerException exceptionMsg) {
        final String returnExceptionMsg = builder.append("空指针异常捕获")
                .append(exceptionMsg.toString()).toString();
        log.error(returnExceptionMsg);
        return returnExceptionMsg;
    }

    @ExceptionHandler(value = BadSqlGrammarException.class)
    @ResponseBody
    public String BadSqlGrammarException(BadSqlGrammarException exceptionMsg) {
        final String returnExceptionMsg = builder.append("sql异常捕获")
                .append(exceptionMsg.toString()).toString();
        log.error(returnExceptionMsg);
        return returnExceptionMsg;
    }

}

